import os
# import pymongo
import requests

# client = pymongo.MongoClient("mongodb://localhost:27017/")
# db = client["率土之滨"]
# collection = db.get_collection('武将库')

hero_url = 'https://g0.gph.netease.com/ngsocial/community/stzb/cfg/gear_id.json?gameid=g10'
hero_response = requests.get(hero_url)
hero_json = hero_response.json()

# collection.insert_one(hero_json)
# client.close()

dir_path = 'static/率土之滨/武器库'
if not os.path.exists(dir_path):
    os.makedirs(dir_path)
else:
    os.removedirs(dir_path)
    os.makedirs(dir_path)

for hero in hero_json:
    # print(hero)
    hero_id = hero['id']
    hero_name = hero['name']

    hero_icon_url = f'https://g0.gph.netease.com/ngsocial/community/stzb/cn/gears/gear_icon/gear_icon_{hero_id}.jpg?gameid=g10'
    hero_icon_response = requests.get(hero_icon_url)

    with open(f'{dir_path}/{hero_name}.png', 'wb') as f:
        f.write(hero_icon_response.content)